package com.ev.k3cloudBusiness.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ev.apis.model.DsResultResponse;
import com.ev.framework.utils.R;
import com.ev.k3cloud.entity.K3CloudBillQueryEntity;
import com.ev.k3cloud.enums.K3CloudBaseFormId;
import com.ev.k3cloud.service.K3CloudService;
import com.ev.k3cloudBusiness.service.K3BDOperatorService;
import com.ev.k3cloudBusiness.vo.K3BDOperatorResultsVo;
import com.ev.k3cloudBusiness.vo.K3BDOperatorWhereVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class K3BDOperatorServiceImpl implements K3BDOperatorService {
    @Autowired
    K3CloudService k3CloudService;
    @Override
    public R OperatorView(K3BDOperatorWhereVo k3BDOperatorWhereVo) throws Exception {
        K3CloudBillQueryEntity k3CloudBillQueryEntity=new K3CloudBillQueryEntity();
        k3CloudBillQueryEntity.setFormId(K3CloudBaseFormId.BD_OPERATOR.getFormId());
        k3CloudBillQueryEntity.setFieldKeys("FEntity_FEntryId,FStaffId.FNAME,FStaffId.FNUMBER,FStaffId,FBizOrgId,FBizOrgId.FNAME,FBizOrgId.FNUMBER");
        String filterString="FIsUse=1";
        if(!k3BDOperatorWhereVo.getOperatorType().equals(""))
        {
            filterString+="and FOperatorType='"+k3BDOperatorWhereVo.getOperatorType()+"'";
        }
        if(!k3BDOperatorWhereVo.getOrgNumber().equals(""))
        {
            filterString+="and FBizOrgId.FNUMBER ='"+k3BDOperatorWhereVo.getOrgNumber()+"'";
        }
        if(!k3BDOperatorWhereVo.getStaffName().equals(""))
        {
            filterString+="and FStaffId.FNAME like'%"+k3BDOperatorWhereVo.getStaffName()+"%'";
        }
        if(!k3BDOperatorWhereVo.getStaffNumber().equals(""))
        {
            filterString+="and FStaffId.FNUMBER like'%"+k3BDOperatorWhereVo.getStaffNumber()+"%'";
        }
        k3CloudBillQueryEntity.setFilterString(filterString);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("data", k3CloudBillQueryEntity);
        String materialResultCount=k3CloudService.billQuery(JSON.toJSONString(dataMap));
        k3CloudBillQueryEntity.setOrderString("FStaffId asc");
        k3CloudBillQueryEntity.setLimit(k3BDOperatorWhereVo.getPagesize());
        k3CloudBillQueryEntity.setStartRow((k3BDOperatorWhereVo.getPapageno()-1)*k3BDOperatorWhereVo.getPagesize());
        String materialResult=k3CloudService.billQuery(JSON.toJSONString(dataMap));
        JSONArray resultObj = JSON.parseArray(materialResult);
        JSONArray resultObjCount = JSON.parseArray(materialResultCount);
        if(materialResult.contains("ResponseStatus"))
        {
            if( !resultObj.getJSONArray(0).getJSONObject(0).getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                log.info(resultObj.getJSONArray(0).getJSONObject(0).getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
                return R.error(resultObj.getJSONArray(0).getJSONObject(0).getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        List<K3BDOperatorResultsVo> k3BDOperatorResultsVoList=new ArrayList<>();
        for(int i=0;i<resultObj.size();i++)
        {
            JSONArray jsonArray=resultObj.getJSONArray(i);
            K3BDOperatorResultsVo k3BDOperatorResultsVo=new K3BDOperatorResultsVo();
            k3BDOperatorResultsVo.setENTRYID(jsonArray.getLong(0));
            k3BDOperatorResultsVo.setFNAME(jsonArray.getString(1));
            k3BDOperatorResultsVo.setFNUMBER(jsonArray.getString(2));
            k3BDOperatorResultsVo.setSTAFFID(jsonArray.getLong(3));
            k3BDOperatorResultsVo.setOrgId(jsonArray.getLong(4));
            k3BDOperatorResultsVo.setOrgName(jsonArray.getString(5));
            k3BDOperatorResultsVo.setOrgNumber(jsonArray.getString(6));
            k3BDOperatorResultsVoList.add(k3BDOperatorResultsVo);
        }
        Map<String,Object> results=new HashMap<>();
        results.put("data",new DsResultResponse(k3BDOperatorWhereVo.getPapageno(), k3BDOperatorWhereVo.getPagesize(), resultObjCount.size(), k3BDOperatorResultsVoList));
        return R.ok(results);
    }
}
